<?php
/**
 * Created by PhpStorm.
 * User: yzh
 * Date: 2019/5/22
 * Time: 11:28
 */

namespace app\api\model;


class WorkUserLog extends BaseModel
{

    public $autoWriteTimestamp = true;

    public function workUser(){
        return $this->belongsTo('WorkUser','wu_id','id')->field('id,name,head_img,position');
    }

    public function thumbsup(){
        return $this->hasMany('WorkUserThumbsup','log_id','id');
    }

    public function getWorkUserInfoAttr($value)
    {
        return json_decode($value);
    }
    /**
     * 批量添加数据
     * @param $data
     * @return array|false
     * @throws \Exception
     */
    public function insertLogs($data){
        $user = new WorkUserLog();
        $result = $user->saveAll($data);
        return $result;
    }

    /**
     * 添加数据
     * @param $data
     * @return WorkUserLog
     */
    public function insertLog($data){
      return self::create($data);
    }

    /**
     * 我的排名
     * @param $wu_id
     * @param $time_info
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function myRank($wu_id,$time_info){
        $result = self::where('wu_id','=',$wu_id)
                ->where('time_info','=',$time_info)
                ->withCount(['thumbsup'])
                ->with(['workUser'=>['userimg']])
                ->find();
       return $result;
    }

    /**
     * 获取排名列表
     * @param $t_id
     * @param $time_info
     * @param $size
     * @param $page
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public static function getList($t_id,$wu_id,$time_info,$page,$size){
        $result = self::where('t_id','=',$t_id)
                ->where('time_info','=',$time_info)
                ->withCount(['thumbsup'])
                ->with(['thumbsup'=>function($query)use($wu_id){
                    $query->where('wu_id','=',$wu_id);
                }])
                ->order('rank asc')
                ->paginate($size,false,['page'=>$page]);

        return $result;
    }






}